package cn.hxzy.controller;

import cn.hxzy.common.R;
import cn.hxzy.entity.Train;
import cn.hxzy.entity.User;
import cn.hxzy.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuankan.WANG
 * @since 2022-01-13
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;

    @PostMapping("/sel")
    public R sel(){
        return userService.sel();
    }


    @PostMapping("/add")
    public R add(@RequestBody User user){
        boolean save = userService.save(user);
        if (!Objects.isNull(save)){
            redisTemplate.opsForValue().set("user",user);
            return R.ok().code(20000).data("item",user).message("保存成功！");
        }
        QueryWrapper wrapper=new QueryWrapper();

        return R.error().message("保存失败！");
    }

    @GetMapping("/update")
    public R update(@RequestParam("userName") String userName){return userService.update(userName);}
}
